//==========================================================================
// Copyright (c) 2000-2008,  Elastos, Inc.  All Rights Reserved.
//==========================================================================
/************************************************************************/
/*                                                                      */
/*              FAT-FTL Lite Software Development Kit                   */
/*              Copyright (C) M-Systems Ltd. 1995-2003                  */
/*                                                                      */
/************************************************************************/

#ifndef FLSYSTEM_H
#define FLSYSTEM_H

#include "flcustom.h" /* Application compilation flags */
#include "flsystyp.h" /* Basic system types            */

/* CPU target
 *
 * Use compiler switches or insert a #pragma here to select the CPU type
 * you are targeting.
 *
 */

/* DiskOnChip window base address
 *
 * Defines the possible location of the DiskOnChip device. You should specify
 * the lowest and highest addresses for the search.
 *
 * The use of those defintion, should be done in your own FLCUSTOM.C file.
 * Those defintions are for your conviniant and are not used by TrueFFS itself.
 *
 * Note: The range in this example is for M-Systems x86 EVB's.
 */

#define FL_LOW_DOC_ADDRESS  0x0000L
#define FL_HIGH_DOC_ADDRESS 0x0000L

/* NULL constant
 *
 * Some compilers require a different definition for the NULL pointer.
 */

#ifndef NULL
#define NULL ((void *)0)
#endif

/* Little-endian/big-endian
 *
 * If your machine uses the big-endian (Motorola) format, uncomment the
 * following line.
 *
 * This compilation flag makes sure that FAT and translation layer structures
 * will be written in little-endian (Intel) format for integers. It also
 * affects the way DiskOnChip 16 bits registers are analized.
 */

/* #define FL_BIG_ENDIAN */

/* H/W Byte swap
 *
 * DiskOnChip devices support the H/W byte swap feature in order to support
 * situations where the device data lines were not properly connected
 * (sometimes happen due to big/little endian design mistakes).
 *
 * The compilation flag will automatically swap every 16 bit access to and
 * from the DiskOnChip device.
 */

/* #define FL_SWAP_BYTES */

/* Memory routines for RAM buffers
 *
 * The library routines memcpy, memset, and memcmp are standard for ANSI-C
 * compilers. However, there may be variations in which a #include defines
 * them, and variations such as the Borland's _fmemcpy, _fmemset, and _fmemcmp
 * routines also exist. To overcome such nuances, the TrueFFS code uses memory
 * handling macros called tffscpy, tffsset, and tffscmp, with prototypes
 * identical to the standard memcpy, memset, memcmp. Provide the correct
 * #include directive, and incorporate C macros that define the tffsxxx
 * routines to the names of your library routines (or user-supplied routines
 * to manually code these routines).
 */

#define tffscpy memcpy
#define tffscmp memcmp
#define tffsset memset

#define TFFS_DLL_API
#define FAR_LEVEL     0
/* Pointer arithmetic
 *
 * The following macros define machine and compiler-dependent macros for
 * handling pointers to physical window addresses. The definitions below are
 * for x86 using Windows XP on an M-Systems EVB.
 *
 * 'physicalToPointer' translates a physical flat address to a pointer.
 * Note that if your processor uses virtual memory, the code should
 * map the physical address to virtual memory, and return a pointer to that
 * memory (the size parameter tells how much memory should be mapped).
 *
 */

/* The include file and pragma bellows are used for opening a memory window to
 * the DiskOnChip device in Windows 2000 and up.
 */

/*
#include "MapMemAPI.h"
#pragma comment(lib, "MapMemory.lib")
#define physicalToPointer(physical,size,drive) (GetPointerFromPhysAddress(physical))

#define addToFarPointer(base,increment)   \
    ((void *) (((unsigned char *)(base)) + (increment)))
#define subFromFarPointer(base,increment)  \
       addToFarPointer(base,(-(long)(increment)))
#define distanceBetweenFarPointers(ptr1, ptr2)  \
    (((unsigned long)(ptr1)) - ((unsigned long)(ptr2)))
#define pointerToPhysical(ptr)  ((unsigned long)(ptr))
#define freePointer(ptr,size)
*/

#if FAR_LEVEL > 0
#include <dos.h>

#define physicalToPointer(physical,size,drive)          \
    MK_FP( ((int)((physical) >> 4)) , ((int) (physical) & 0xF) )

#define pointerToPhysical(ptr)                  \
    (((unsigned long) FP_SEG(ptr) << 4) + FP_OFF(ptr))

#define addToFarPointer(base,increment)                  \
        MK_FP((FP_SEG(base) +                            \
        ((unsigned short) ((FP_OFF(base) + (increment)) >> 16) << 12)), \
          (FP_OFF(base) + (int) (increment)))

#define freePointer(ptr,size)

#else /* FAR_LEVEL > 0 */

#define physicalToPointer(physical,size,drive)          \
    ((void *) (physical))

#define pointerToPhysical(ptr)  ((unsigned long)(ptr))

#define addToFarPointer(base,increment)         \
    ((void *) ((unsigned char *) (base) + (increment)))

#define freePointer(ptr,size)

#endif /* FAR_LEVEL > 0 */

/*
 * Signed/unsigned char
 *
 * It is assumed that 'char' is signed. If this is not your compiler
 * default, use compiler switches, or insert a #pragma here to define this.
 *
 */

/* Mutex type
 *
 * If you intend to access the TrueFFS API in a multi-tasking environment,
 * you may need to implement some resource management and mutual-exclusion
 * of TrueFFS with mutex & semaphore services that are available to you. In
 * this case, define here the Mutex type you will use, and provide your own
 * implementation of the Mutex functions in flcustom.c
 *
 * By default, a Mutex is defined as a simple counter, and the Mutex
 * functions in flcustom.c implement locking and unlocking by incrementing
 * and decrementing the counter. This will work well on all single-tasking
 * environments, as well as on many multi-tasking environments.
 */

typedef int FLMutex;

/* Memory allocation
 *
 * Some modules in TrueFFS initialization routines need to allocate
 * memory to handle the Flash media. The required size depends on the media
 * being handled.
 *
 * You may choose to use the standard 'malloc' and 'free' to handle such
 * memory allocations, provide your own equivalent routines, or you may
 * choose not to define any memory allocation routine. In this case, the
 * memory will be allocated statically at compile-time on the assumption of
 * the largest media configuration you need to support. This is the simplest
 * choice, but may cause your RAM requirements to be larger than you
 * actually need.
 *
 * If you define routines other than malloc & free, they should have the
 * same parameters and return types as malloc & free. You should either code
 * these routines in flcustom.c or include them when you link your application.
 *
 * In case you do not have dynamic allocation resources, then you can use the
 * TrueFFS implementation. You will need to set the FL_TRUEFFS_HEAP_SIZE
 * definition in FLCUSTOM.H and add FLMALLOC.C to your application or driver.
 */

#if FL_TRUEFFS_HEAP_SIZE > 0
#include "flmalloc.h"
#define FL_MALLOC    flMalloc
#define FL_FREE(a)   flFree((void *)a)
#else /* FL_TRUEFFS_HEAP_SIZE > 0*/
#define FL_MALLOC    malloc
#define FL_FREE(a)   free((void *)a)
#endif /* FL_TRUEFFS_HEAP_SIZE > 0*/

/* Debug mode
 *
 * Customize the various TrueFFS debug print macros, to suite your application needs.
 */

#define DBG_PRINT_ERR(zone,str)      printk (str)
#define DBG_PRINT_ERR_PRM(zone,str)  printk str
#define DBG_PRINT_WRN(zone,str)      /*printk (str)*/
#define DBG_PRINT_WRN_PRM(zone,str)  /*printk str*/
#define DBG_PRINT_FLOW(zone,str)     /* printk (str) */
#define DBG_PRINT_FLOW_PRM(zone,str) /* printk str   */

/*
 * First IOCTL function number
 *
 * When using TrueFFS' IOCTL functions you have to define the code of the first
 * TrueFFS IOCTL function (after that the functions get consecutive increasing numbers).
 * This number should be out of the range of the standard IOCTL codes used by your
 * operating system.
 *
 */

#ifdef FL_IOCTL_INTERFACE
#define FL_IOCTL_START  0
#endif

/* Default calling convention
 *
 * C compilers usually use the C calling convention to routines (cdecl), but
 * often can also use the pascal calling convention, which is somewhat more
 * economical in code size. Some compilers also have specialized calling
 * conventions which may be suitable. Use compiler switches or insert a
 * #pragma here to select your favorite calling convention.
 */

/* Naming convention for functions that uses non-default convention.
 *
 * In case the calling application uses a different convention then the one
 * used to compile TrueFFS you can use the NAMING_CONVENTION definition.
 * The NAMING_CONVENTION definition is added as a qualifier to all TrueFFS
 * exported API. A good example is a c++ application that uses TrueFFS,
 * which was compiled using standard C.If this is not the case simply leave
 * the NAMING_CONVENTION defined as empty macro.
 */

/* #define NAMING_CONVENTION cdecl */

/* System clean up routine
 *
 * This routine will be called every time TrueFFS exists.
 * The implementation is found in flsystem.c
 */

extern void flSysfunExit(void);

#define FL_SYS_FUNC_RELEASE flSysfunExit()

/* Control over the priority of the TrueFFS thread
 *
 * Some of TrueFFS time is spend on polling the flash ready\busy signal.
 * TrueFFS supplies a verity of ways to utilize this time, one of which is
 * to inform the OS that TrueFFS priority can be lowered for the time being.
 */

#define FL_LowerPriorityThread(flashPtr)
#define FL_RaisePriorityThread(flashPtr)

/* TrueFFS Yield CPU routine
 *
 * Allows the CPU to process other tasks while TrueFFS is waiting for the
 * flash/DiskOnChip to complete an operation. The estimated amount of time,
 * is supplied as an argument.
 * The amount of time spend in the routine can be longer or shorted then
 * the given argument.
 *
 * Note: This routine can be used in conjunction with the interrupt mechanism.
 */

#define FL_TFFS_SLEEP(handle,microseconds) /* Do nothing - simply pole the R/B */

/* TrueFFS delay routine
 *
 * Delays execution for a number of milliseconds.
 * If there is no msec-accuracy to the clock, this routine should wait
 * at least the time specified.
 *
 * This routine may be called from the socket interface or MTD's, and
 * is not necessary for all implementations.
 *
 * Parameters:
 *      milliseconds        : Milliseconds to wait
*/

#define FL_DELAY_MSEC(a) udelay(3*a)

/* Improving performance by using native code.
 *
 * You can improve the way TrueFFS utilizes 32bit (or larger) native variables
 * by uncommenting the definition bellow.
 */

/* #define FL_ASSUME_NATIVE_IS_32BITS */

/* Removing backward compatibility basic type definitions.
 *
 * TrueFFS 5.x used the byte, word, dword, Sbyte, Sword and Sdword basic types.
 * Since these names are used by several major OSs, such as Windows and
 * VxWorks, it was decided to rename these types with less general names
 * (See the FLxxx types below). As many TrueFFS-based applications already
 * use these types, TrueFFS still automatically defines them. If these
 * definitions cause your compiler to report redefinition errors, simple
 * define FL_DISABLE_OLD_TRUEFFS_TYPES and TrueFFS will automatically remove
 * the definition. */

/* #define FL_DISABLE_OLD_TRUEFFS_TYPES */

/* Optionally you can define TrueFFS basic types:
 *
 * FLByte, FLWord, FLDword, FLSByte, FLSWord, FLSDword,
 */

/* Initialization for H/W configuration routine
 *
 * Some of the DiskOnChip H/W properties can be configured using the
 * flHWConfig() routine.
 *
 * However it is usually more convenient to just code the TrueFFS defaults here
 * and save the trouble of calling the actual routine.
 *
 * #define FL_IRQ_RB_INIT(wSockeNo)             FL_INT_RB_DISABLED
 * #define FL_DPD_INIT(wSockeNo)    FL_DPD_DISABLED
 * #define FL_PAGE_MODE_INIT(wSockeNo)   FL_DISABLED
 * #define FL_TURBO_MODE_INIT(wSockeNo)   FL_NONTURBO_MODE
 * #define FL_DMA_INIT(wSockeNo)    FL_DMA_HW_DISABLED
 * #define FL_DPS_2_COPIES_INIT(wSockeNo)       FL_DISABLED
 */

/************************************************************************************/
/*************************   Access Layer Customization   ***************************/
/************************************************************************************/

/*********************************/
/* Runtime customization options */
/*********************************/

/*
 * Uncomment the FL_NO_INIT_MMU_PAGES definition for:
 *
 * In order to skip initialization of first and last byte of the given buffer.
 * When the user buffer resides on separated memory pages the read
 * operation may cause a page fault. Some CPU's return from a page
 * fault (after loading the new page) and reread the bytes that caused
 * the page fault from the new loaded page. In order to prevent such a
 * case the first and last bytes of the buffer are written.
 *
 */

/* #define FL_NO_INIT_MMU_PAGES */

/**************************************/
/* Compile time customization options */
/**************************************/

/* Remove runtime control over memory access routines
 *
 * When defined , TrueFFS will use your own macros , for accessing the DiskOnChip memory window
 * This implementation will provide better performance, at the expense of automatic detection
 * and ease of integration.
 *
 * Default macros can be found in _DOCSYS.H
 *
 * #define flDirectWrite8BitReg(flash,offset,val)           : Write 8  bit val to flash->win+offset
 * #define flDirectWrite16BitReg(flash,offset,val)          : Write 16  bit val to flash->win+offset
 * #define flDirectRead8BitReg(flash,offset)                : Read 8  bit val from flash->win+offset
 * #define flDirectRead16BitReg(flash,offset)               : Read 8  bit val from flash->win+offset
 * #define flReadEvenNumberOfBytes(flash,offset,dest,count) : Read count bytes from flash->win+offset to dest
 * #define flRead512Bytes(flash,offset,dest) <= (S/W DMA)   : Same as above only 512 bytes
 * #define flWriteEvenNumberOfBytes(flash,offset,src,count) : Write count bytes to flash->win+offset from dest
 * #define flWrite512Bytes(flash,offset,src) <= (S/W DMA)   : Same as above only 512 bytes
 * #define flDocWindow(flash)                               : DiskOnChip memory window (usually 2KB)
 */

/* #define FL_NO_USE_FUNC */

/* Physical access to the device.
 *
 * While the routines above return either actual data to the higher layer S/W
 * the routines bellow provides the means for those accesses.
 *
 * Note: Default macros can be found in FLCHKDEFS.H
 *
 * FLWRITE_IO_BYTE        , FLREAD_IO_BYTE
 * FLWRITE_IO_WORD        , FLREAD_IO_WORD
 * FLWRITE_IO_DWORD       , FLREAD_IO_DWORD
 * TFFSCPY_FROM_IO_8_BITS , TFFSCPY_FROM_IO_16_BITS
 * TFFSCPY_TO_IO_8_BITS   , TFFSCPY_TO_IO_16_BITS
 * TFFSSET_IO_8_BITS      , TFFSSET_IO_16_BITS
 */

/* The G3 DiskOnChip supports the following access times:
 * - SRAM     access time (slowest)
 * - NonTurbo access time (faster)
 * - Turbo    access time (fastest)
 * Costumer has the following options:
 * - The access time to DiskOnChip is always SRAM access time
 * 1. Configure DOC to work at access time equal or slower than SRAM access time
 *    in that case do not define the following macro.
 * 2. Provide routine that enables TFFS to change the access time in run-time
 *    and hence to provide fast access time to registers
 */

/* #define FL_CHANGE_READ_ACCESS_TIME(wTimeNanoSec,bSocket) */

/* Burst mode configuration
 *
 * The following configurations are required for activating the burst support
 * of the DiskOnChip.
 */
/* #define FL_USE_BURST_MODE_READ */

#ifdef FL_USE_BURST_MODE_READ
#define BURST_LENGTH  3 /* 2^3 = 8 Words */
#define BURST_LATENCY 0
#define BURST_CLK_INV 0
#define BURST_HOLD    0

#define FL_HOST_ENTER_BURST_MODE flHostEnterBurstMode
#define FL_HOST_EXIT_BURST_MODE  flHostExitBurstMode
#endif /* FL_USE_BURST_MODE_READ */

/************************/
/* HW DMA Configuration */
/************************/

/* #define FL_DMA_CONFIG(bDMA_ParamsPtr) */

#endif /* FLSYSTEM_H */
